﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SnowDAL;
using Common.Expansion;
using SnowEntity;
using System.Data;
using Common;

namespace Snow
{
    public partial class Topic : PageBase
    {
        private TopicDAL _dal = new TopicDAL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        private void DataBindTopic()
        {
            int module = ddlModule.SelectedValue.TryInt();
            gvTopic.DataSource = _dal.QueryTopicByModule(module);
            gvTopic.DataBind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            DataBindTopic();
        }

        protected void gvTopic_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            _dal.DeleteTopic(DataKeyValue("tid", gvTopic.Rows[e.RowIndex]));
            gvTopic.EditIndex = -1;
            DataBindTopic();
        }

        protected void gvTopic_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            DataTable dt = _dal.QueryTopic(DataKeyValue("tid", gvTopic.Rows[e.NewSelectedIndex]));

            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                lblTid.Text = row["tid"].ToString();
                txtUid.Text = row["uid"].ToString();
                txtTitle.Text = row["title"].ToString();
                txtCount.Text = row["content"].ToString();
                txtTime.Text = row["time"].ToString();
                txtUV.Text = row["uv"].ToString();
                ddlModuleA.SelectedValue = row["module"].ToString();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUid.Text.Trim()))
            {
                Page.Alert("信息不全");
                return;
            }

            Topics topic = new Topics();
            topic.Uid = txtUid.Text.Trim().TryInt();
            topic.Title = txtTitle.Text;
            topic.Content = Utility.FormatString(txtCount.Text);
            topic.Time = txtTime.Text;
            topic.Module = ddlModuleA.SelectedValue.TryInt();
            topic.UV = txtUV.Text.TryInt();
            _dal.InsertTopic(topic);
            DataBindTopic();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            _dal.UpdateTopic(
                lblTid.Text,
                txtUid.Text,
                txtTitle.Text,
                txtCount.Text,
                txtTime.Text,
                ddlModuleA.SelectedValue,
                txtUV.Text);

            DataBindTopic();
        }

        private string DataKeyValue(string key, GridViewRow row)
        {
            return gvTopic.DataKeys[row.RowIndex].Values[key].ToString();
        }

        protected void gvTopic_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTopic.PageIndex = e.NewPageIndex;
            DataBindTopic();
        }


    }
}